{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import urllib.request\n",
    "import urllib,sys\n",
    "import ssl"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b'{\"refresh_token\":\"25.1f3e10bd56da78c0b01978af38ea3575.315360000.1926785773.282335-23579498\",\"expires_in\":2592000,\"session_key\":\"9mzdWWM2\\\\/P2cN1Iq7xEyZl64Uzd5xcuAs62DDEbatH87Rsh08uC3Trhf9GW8lxslo1bglDLQf35BNXZJaQS8s1wcEPWl5g==\",\"access_token\":\"24.b9eeb13d218de468b94a02b64d73fd37.2592000.1614017773.282335-23579498\",\"scope\":\"audio_voice_assistant_get brain_enhanced_asr audio_tts_post brain_speech_realtime public nlp_simnet nlp_wordemb nlp_comtag nlp_dnnlm_cn brain_nlp_lexer brain_all_scope brain_nlp_comment_tag brain_nlp_dnnlm_cn brain_nlp_word_emb_vec brain_nlp_word_emb_sim brain_nlp_sentiment_classify brain_nlp_simnet brain_nlp_depparser brain_nlp_wordembedding brain_nlp_dnnlm_cn_legacy brain_nlp_simnet_legacy brain_nlp_comment_tag_legacy brain_nlp_lexer_custom brain_nlp_keyword brain_nlp_topic picchain_test_picchain_api_scope brain_nlp_ecnet brain_nlp_emotion brain_nlp_comment_tag_custom brain_nlp_news_summary brain_nlp_sentiment_classify_custom brain_nlp_address brain_asr_async wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test\\\\u6743\\\\u9650 vis-classify_flower lpq_\\\\u5f00\\\\u653e cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base smartapp_mapp_dev_manage iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_\\\\u5f00\\\\u653eScope vis-ocr_\\\\u865a\\\\u62df\\\\u4eba\\\\u7269\\\\u52a9\\\\u7406 idl-video_\\\\u865a\\\\u62df\\\\u4eba\\\\u7269\\\\u52a9\\\\u7406 smartapp_component smartapp_search_plugin\",\"session_secret\":\"5eac869c23550e2021c8f0b0b30edffa\"}\\n'\n"
     ]
    }
   ],
   "source": [
    "host = 'https://aip.baidubce.com/oauth/2.0/token?' \\\n",
    "       'grant_type=client_credentials&client_id=oYLGtS625Sx3juL36HA8SGqh&client_secret=a7Rg5WjQYXnE7zF1Pmc2PE8H7nEaIj5c'\n",
    "request = urllib.request.Request(host)\n",
    "request.add_header('Content-Type', 'application/json; charset=UTF-8')\n",
    "response = urllib.request.urlopen(request)\n",
    "content = response.read()\n",
    "if (content):\n",
    "    print(content)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原文： 百度是一家人工只能公司\n",
      "纠错后： 百度是一家人工智能公司\n",
      "Score： 0.797593\n"
     ]
    }
   ],
   "source": [
    "# -*- coding: utf-8 -*-\n",
    "#!/usr/bin/env python\n",
    "\n",
    "import urllib\n",
    "import json\n",
    "#Access Token的有效期为30天（以秒为单位），请您集成时注意在程序中定期请求新的token\n",
    "\n",
    "#client_id 为官网获取的AK， client_secret 为官网获取的SK\n",
    "\n",
    "client_id ='oYLGtS625Sx3juL36HA8SGqh'\n",
    "client_secret ='a7Rg5WjQYXnE7zF1Pmc2PE8H7nEaIj5c'\n",
    "\n",
    "#获取token\n",
    "def get_token():\n",
    "    host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=' + client_id + '&client_secret=' + client_secret\n",
    "    request = urllib.request.Request(host)\n",
    "    request.add_header('Content-Type', 'application/json; charset=UTF-8')\n",
    "    response = urllib.request.urlopen(request)\n",
    "    token_content = response.read()\n",
    "    if token_content:\n",
    "        token_info = json.loads(token_content)\n",
    "        token_key = token_info['access_token']\n",
    "    return token_key\n",
    "\n",
    "\n",
    "def txt_correction(content):\n",
    "    print('原文：', content)\n",
    "    token = get_token()\n",
    "    url = 'https://aip.baidubce.com/rpc/2.0/nlp/v1/ecnet'\n",
    "    params = dict()\n",
    "    params['text'] = content\n",
    "    params = json.dumps(params).encode('utf-8')\n",
    "    access_token = token\n",
    "    url = url + \"?access_token=\" + access_token\n",
    "    request = urllib.request.Request(url=url, data=params)\n",
    "    request.add_header('Content-Type', 'application/json')\n",
    "    response = urllib.request.urlopen(request)\n",
    "    content = response.read()\n",
    "    if content:\n",
    "        content = content.decode('GB2312')\n",
    "        data = json.loads(content)\n",
    "\n",
    "        item = data['item']\n",
    "        print('纠错后：', item['correct_query'])\n",
    "        print('Score：', item['score'])\n",
    "\n",
    "\n",
    "txt_correction('百度是一家人工只能公司')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
